/* 
 * File:   net_server.h
 * Author: james
 *
 * Created on 2011年3月31日, 下午12:56
 */

#ifndef NET_SERVER_H
#define	NET_SERVER_H

#include <boost/asio.hpp>
#include <string>
#include <boost/noncopyable.hpp>
#include <boost/pool/object_pool.hpp>
#include <boost/thread/thread.hpp>

#include "message.h"
#include "connection.h"

namespace edm
{
    namespace network
    {
        //! network server service support multithread handle
        class net_server_t: private boost::noncopyable
        {
        protected:
            net_server_t(const std::string& host, const std::string port, unsigned int concurrency);
            ~net_server_t();
                
        public:
            //! start network server
            int start(callback_handler_t cb, callback_error_t cb_err, callback_connect_t cb_connect);
            
            //! stop network server
            int stop();
        private:
            //! accept callback
            void handle_accept(const boost::system::error_code& error);
        protected:
            //! io service object of boost for deadline time and thread working
            boost::asio::io_service m_io_service;
        private:
            //! network server bind host
            std::string         m_host;
            //! network server bind port
            std::string         m_port;
            //! network server thread concurrency
            unsigned int        m_concurrency;
            //! identify whether service is running
            bool m_started;
            //! callback_function_type when message event trigger
            callback_handler_t  m_callback_handler;
            //! callback_function_type when error event trigger
            callback_error_t    m_callback_error;
            //! callback_function_type when connect event trigger
            callback_connect_t  m_callback_connect;
            //! io service work object of boost for keeping io service is running
            boost::scoped_ptr<boost::asio::io_service::work> m_io_work;
            //! acceptor used to listen for incoming connections.
            boost::asio::ip::tcp::acceptor m_acceptor;
            //! connection ptr for new acccepted connection
            connection_ptr_t m_new_connection;
            //! thread pool
            boost::thread_group m_thread_group;
            //! connection ptr pool
            //! boost::object_pool<connection_t> m_connection_memory_pool;
        };
    
    }
}

#endif	/* NET_SERVER_H */

